package com.demo.ctf;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class CmpleteExceptionallyDemo {
    public static void main(String[] args) {
        CompletableFuture<String> future = new CompletableFuture<>();

        // 模拟异步操作失败，使用 completeExceptionally 来标记 future 为完成状态并附带异常
        future.completeExceptionally(new RuntimeException("异步操作失败"));

        try {
            // 尝试获取 future 的结果，将会抛出 ExecutionException，因为 future 是以异常完成的
            String result = future.get();
            System.out.println(result);
        } catch (InterruptedException | ExecutionException e) {
            // 打印出原始异常信息
            e.printStackTrace();
        }
    }
}